package com.bc.bcupload.controller;

import com.bc.bcupload.common.ResultModel;
import com.bc.commoncore.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.bc.bcupload.common.UploadFileHelper;

/**
 * @Name FileUploadController
 * @Description
 * @Author douhaichao
 * @Date 2018/8/14 16:11
 * @Version 1.0
 **/
@Controller
@RequestMapping("/")
public class FileUploadController {

    //file_type参数必填
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        List<ResultModel> result = UploadFileHelper.uploadFile(request);
        if (result.size() == 1) {
            return JsonUtil.obj2string(result.get(0));
        } else if (result.size() > 0) {
            return JsonUtil.obj2string(result);
        }
        return null;
    }
}
